import { NodeStatusEnum } from '../app/type';
import { ChatHistoryItemResType } from '../chat/type';

import { FlowNodeTemplateTypeEnum, FlowNodeTypeEnum } from './constant';
import { FlowNodeInputItemType, FlowNodeOutputItemType } from './io';

export type FlowNodeCommonType = {
  parentNodeId?: string;
  flowNodeType: FlowNodeTypeEnum; // render node card
  abandon?: boolean; // abandon node

  avatar?: string;
  name: string;
  intro?: string; // template list intro
  showStatus?: boolean; // chatting response step status
  version?: string;

  // data
  inputs: FlowNodeInputItemType[];
  outputs: FlowNodeOutputItemType[];

  // plugin data
  pluginId?: string;
  isFolder?: boolean;

  status?: NodeStatusEnum;
  // pluginType?: AppTypeEnum;
};

type HandleType = {
  left: boolean;
  right: boolean;
  top: boolean;
  bottom: boolean;
};
// system template
export type FlowNodeTemplateType = FlowNodeCommonType & {
  id: string; // node id, unique
  templateType: FlowNodeTemplateTypeEnum;

  // show handle
  sourceHandle?: HandleType;
  targetHandle?: HandleType;

  // info
  isTool?: boolean; // can be connected by tool

  // action
  forbidDelete?: boolean; // forbid delete
  unique?: boolean;

  diagram?: string; // diagram url
  courseUrl?: string; // course url
  isFolded?: boolean; // 是否折叠
};

export type ParentIdType = string | null | undefined;

export type ParentTreePathItemType = {
  parentId: string;
  parentName: string;
};

export type NodeTemplateListItemType = {
  id: string; // 系统节点-系统节点的 id， 系统插件-插件的 id，团队应用的 id
  flowNodeType: FlowNodeTypeEnum; // render node card
  parentId?: ParentIdType;
  isFolder?: boolean;
  templateType: FlowNodeTemplateTypeEnum;
  avatar?: string;
  name: string;
  intro?: string; // template list intro
  isTool?: boolean;
  authorAvatar?: string;
  author?: string;
  unique?: boolean; // 唯一的
  currentCost?: number; // 当前积分消耗
  type?: string;
  pluginId?: string;
};

export type NodeTemplateListType = {
  type: FlowNodeTemplateTypeEnum;
  label: string;
  list: NodeTemplateListItemType[];
}[];

// react flow node type
export type FlowNodeItemType = FlowNodeTemplateType & {
  nodeId: string;
  parentNodeId?: string;
  isError?: boolean;
  debugResult?: {
    status: 'running' | 'success' | 'skipped' | 'failed';
    message?: string;
    showResult?: boolean; // show and hide result modal
    response?: ChatHistoryItemResType;
    isExpired?: boolean;
  };
  isFolded?: boolean;
};

// store node type
export type StoreNodeItemType = FlowNodeCommonType & {
  nodeId: string;
  // isEntry: boolean;
  position?: {
    x: number;
    y: number;
  };
};

export type WorkflowCheckListNodeResultType = {
  nodeId: string;
  nodeName: string;
  avatar?: string;
  nodeType: FlowNodeTypeEnum;
  isValid: boolean;
  checkResults: {
    type: 'system' | 'input' | 'connection' | 'ifElse' | 'reference' | 'llm';
    isValid: boolean;
    message: string;
    details?: any;
  }[];
  summary: {
    totalChecks: number;
    validChecks: number;
    invalidChecks: number;
  };
  missingVariableNames: string[];
};

export type WorkflowCheckListType = {
  isValid: boolean;
  nodeResults: WorkflowCheckListNodeResultType[];
  globalSummary: {
    totalNodes: number;
    validNodes: number;
    invalidNodes: number;
    totalChecks: number;
    validChecks: number;
    invalidChecks: number;
  };
  invalidNodes: {
    nodeId: string;
    nodeName: string;
    nodeType: FlowNodeTypeEnum;
    errorTypes: string[];
    missingVariableNames: string[];
  }[];
  allMissingVariableNames: string[];
};
